﻿using ActiveStoryTouch.DataModel;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using ActiveStoryTouch.DataModel.Helpers;
using System.IO;
using System.Xml.Serialization;
using System.Runtime.Serialization;

namespace TestSuite.DataModel
{
    
    
    /// <summary>
    ///This is a test class for PrototypeElement and is intended
    ///to contain all PrototypeElement Unit Tests
    ///</summary>
    [TestClass()]
    public class PrototypeElementTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///A test for PrototypeElement Constructor
        ///</summary>
        [TestMethod()]
        public void PrototypeElementConstructorTest()
        {
            PrototypeElement element = new PrototypeElement { ElementType = ElementTypesHelper.ElementTypeFromString(CommonTestConstants.TestElementTypeString), Top = 100.0, Left = 200.0, Width = 120.0, Height = 90.0, Content = "Test", ContentVisibile = false, HoverGlow = false, UniqueId = 1 };
            Assert.AreEqual<ElementTypes>(CommonTestConstants.TestElementType, element.ElementType);
            Assert.AreEqual<double>(100.0, element.Top);
            Assert.AreEqual<double>(200.0, element.Left);
            Assert.AreEqual<double>(120.0, element.Width);
            Assert.AreEqual<double>(90.0, element.Height);
            Assert.AreEqual<String>("Test", element.Content);
            Assert.AreEqual<bool>(false, element.ContentVisibile);
            Assert.AreEqual<bool>(false, element.HoverGlow);
            Assert.AreEqual<long>(1, element.UniqueId);

            GestureManager gestureManager = GestureManager.LoadFromFile(Path.Combine(CommonTestConstants.RelativeApplicationDirectory, CommonTestConstants.GestureListFileName));
            Assert.IsNotNull(gestureManager);

            string gestureIdentifier = gestureManager.KeyList[1];

            element.GestureTargetPageMap.Add(gestureIdentifier, 1);

            Assert.AreNotEqual<Dictionary<String, long>>(null, element.GestureTargetPageMap);
            Assert.AreEqual<long>(1, element.GestureTargetPageMap[gestureIdentifier]);
        }

        /// <summary>
        /// A test for serializing a list of PrototypeElement objects
        /// </summary>
        [TestMethod()]
        public void PrototypeElementSerializationTest()
        {
            PrototypeElement element = new PrototypeElement { ElementType = ElementTypesHelper.ElementTypeFromString(CommonTestConstants.TestElementTypeString), Top = 100.0, Left = 200.0, Width = 120.0, Height = 90.0, Content = "Test", ContentVisibile = false, HoverGlow = false, UniqueId = 1 };
            GestureManager gestureManager = GestureManager.LoadFromFile(Path.Combine(CommonTestConstants.RelativeApplicationDirectory, CommonTestConstants.GestureListFileName));
            Assert.IsNotNull(gestureManager);

            string gestureIdentifier = gestureManager.KeyList[1];

            element.GestureTargetPageMap.Add(gestureIdentifier, 1);

            PrototypeElement elementTwo = new PrototypeElement { ElementType = ElementTypes.ListBox, Top = 50.0, Left = 40.0, Width = 20.0, Height = 70.0, Content = "Test two!", ContentVisibile = true, HoverGlow = true, UniqueId = 2 };
            List<PrototypeElement> prototypeElementList = new List<PrototypeElement>();
            prototypeElementList.Add(element);
            prototypeElementList.Add(elementTwo);
            DataContractSerializer serializer = new DataContractSerializer(typeof(List<PrototypeElement>));
            FileStream fileStream = new FileStream(CommonTestConstants.ProtoListTestFileName, FileMode.Create);
            serializer.WriteObject(fileStream, prototypeElementList);
            fileStream.Flush();
            fileStream.Close();

            fileStream = new FileStream(CommonTestConstants.ProtoListTestFileName, FileMode.Open);
            List<PrototypeElement> listFromFile = (List<PrototypeElement>)serializer.ReadObject(fileStream);
            fileStream.Close();

            Assert.AreEqual<int>(prototypeElementList.Count, listFromFile.Count);
            Assert.AreEqual<long>(prototypeElementList[0].UniqueId, listFromFile[0].UniqueId);
            Assert.AreEqual<long>(prototypeElementList[1].UniqueId, listFromFile[1].UniqueId);
        }
        [TestMethod()]
        public void PrototypeElementDeserializationTest()
        {
            Assert.Fail("No test impelemented!");
        }
    }
}
